﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.Versioning;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Xml.Linq;


//namespace CreateProjectBotice
//{
//    public class Program
//    {
//        public static void Main(string[] args)
//        {
//            CreateProject();
//        }
//        public static void CreateProject()
//        {
//            //创建项目
//            var projInfo = new Project { ProjCode = "1001", ProjName = "测试项目通知" };
//            Console.WriteLine("创建项目" + projInfo.ToString());
//            //保存
//            Console.WriteLine("保存项目");
//            var resultArr = GetInstances();
//            foreach (NotifiedBase m in resultArr)
//            {
//                //通知
//                m.notice(projInfo);

//            }

//        }

//        public static List<NotifiedBase> GetInstances()
//        {
//            string[] arrayInstance;
//            arrayInstance = new string[] { "CreateProjectBotice.Program+NotifiedFI", "CreateProjectBotice.Program+NotifiedRM" };
//            List<NotifiedBase> FList = new List<NotifiedBase>();
//            foreach (string m in arrayInstance)
//            {
//                Type type = Type.GetType(m);
//                var result = (NotifiedBase)Activator.CreateInstance(type);
//                FList.Add(result);

//                //通知
//            }
//            return FList;

//        }

//        public class NotifiedBase
//        {
//            public virtual void notice(Project param)
//            {
//                Console.WriteLine("通知: " + param);
//            }
//        }
//        public class NotifiedFI : NotifiedBase
//        {
//            public override void notice(Project param)
//            {
//                Console.WriteLine("财务系统接收通知:"+param);
//            }

//        }
//        public class NotifiedRM : NotifiedBase
//        {
//            public override void notice(Project param)
//            {
//                Console.WriteLine("科研系统接收通知 :" + param);
//            }

//        }
//        public class NotifiedHR : NotifiedBase
//        {
//            public override void notice(Project param)
//            {
//                Console.WriteLine("人事系统接收通知 :" + param);
//            }

//        }
//        public class Project
//        {
//            public string ProjName { get; set; }
//            public string ProjCode { get; set; }
//            public override string ToString()
//            {
//                return "Project: " + ProjName + " " + ProjCode;
//            }

//        }



//    }
//}




namespace CreateProjectBotice
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateProject();
        }
        public static void CreateProject()
        {
            //创建项目
            var projInfo = new Project { ProjCode = "1003", ProjName = "测试单例模式项目通知3" };
            Console.WriteLine("创建项目" + projInfo.ToString());
            //保存
            Console.WriteLine("保存项目");
            var resultArr = GetInstances();
            foreach (var item in resultArr)
            {
                //通知
                item.Value.notice(projInfo);
            }
        }

        public static Dictionary<string, NotifiedBase> GetInstances()
        {
            Dictionary<string, NotifiedBase> dict = new Dictionary<string, NotifiedBase>();
            string[] arrayInstance;
            arrayInstance = new string[] { "CreateProjectBotice.Program+NotifiedFI", "CreateProjectBotice.Program+NotifiedRM" };
            foreach (string item in arrayInstance)
            {
                Type type = Type.GetType(item);
                if (!dict.ContainsKey(item))
                { //如果无
                    lock (_lock) {
                        if (!dict.ContainsKey(item))
                        {
                             resultInstance = (NotifiedBase)Activator.CreateInstance(type);
                        }
                    }
                }
                dict.Add(item, resultInstance);

                //通知
            } 
            return dict;

        }
        public static NotifiedBase resultInstance;
        public static object _lock = new object();
   
        public class NotifiedBase
        {
            public virtual void notice(Project param)
            {
                Console.WriteLine("通知: " + param);
            }
        }
        public class NotifiedFI : NotifiedBase
        {
            public override void notice(Project param)
            {
                Console.WriteLine("财务系统接收通知:" + param);
            }

        }
        public class NotifiedRM : NotifiedBase
        {
            public override void notice(Project param)
            {
                Console.WriteLine("科研系统接收通知 :" + param);
            }

        }
        public class NotifiedHR : NotifiedBase
        {
            public override void notice(Project param)
            {
                Console.WriteLine("人事系统接收通知 :" + param);
            }

        }
        public class Project
        {
            public string ProjName { get; set; }
            public string ProjCode { get; set; }
            public override string ToString()
            {
                return "Project: " + ProjName + " " + ProjCode;
            }

        }



    }
}
